package com.cashmate.struts;

import com.cashmate.hibernate.dao.CreditinvestigationDAO;
import com.cashmate.hibernate.pojo.Creditinvestigations;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;

public class EditCreditinvestigation extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditCreditinvestigation.class);
    //This properties have setters below... The setters are called and populated by the grid.
    private String id;//the column in grid which we set key=true
    private String nickname;
    private String addressfloorarea;
    private String addresslotarea;
    private String addressstability;
    private String addressmodel;
    private String addressmake;
    private String addressland;
    private String addressmonthlyrental;
    private String addresslandlordname;
    private String addresslandlordaddress;
    private String addresslandlordphoneno;
    private String spousedegree;
    private String spouseschool;
    private String spouseyeargraduated;
    private String previousstreetaddress;
    private String previousbarangay;
    private String previouscityormunicipality;
    private String previousprovince;
    private String previouspostalcode;
    private String previouslengthofstayinyears;
    private String previouslengthofstayinmonths;
    private String parentsstreetaddress;
    private String parentsbarangay;
    private String parentscityormunicipality;
    private String parentsprovince;
    private String parentspostalcode;
    private String parentslengthofstayinyear;
    private String parentslengthofstayinmonths;
    private String fatherinlawfirstname;
    private String fatherinlawmiddlename;
    private String fatherinlawlastname;
    private String motherinlawfirstname;
    private String motherinlawmiddlename;
    private String motherinlawlastname;
    private String fatherinlawemployer;
    private String motherinlawemployer;
    private String fatherinlawposition;
    private String motherinlawposition;
    private String fatherinlawage;
    private String motherinlawage;
    private String parentsinlawstreetaddress;
    private String parentsinlawbarangay;
    private String parentsinlawcityormunicipality;
    private String parentsinlawprovince;
    private String parentsinlawpostalcode;
    private String parentsinlawlengthofstayinyears;
    private String parentsinlawlengthofstayinmonths;
    private String presentaddresslengthofstayinyears;
    private String presentaddresslengthofstayinmonths;
    private String presentaddressstatus;
    private CreditinvestigationDAO crdao = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    //Notice we don't have a success result because we do not need to redirect the user when he/she updates the dbase
    //We redirect him/her only if an error occurred.
    @Action(value = "/edit-creditinv", results = {
        @Result(name = "error", type = "redirectAction", location = "../error/messages.jsp")
    })
    @Override
    public String execute() throws Exception {
        Creditinvestigations c = crdao.get(Integer.parseInt(id));
        try {
            String fieldname = "";
            String fieldvalue = "";
            if (nickname != null) {
                c.setNickname(nickname);
                fieldname = "nickname";
                fieldvalue = nickname;
            }
            if (addressfloorarea != null) {
                c.setAddressfloorarea(addressfloorarea);
                fieldname = "addressfloorarea";
                fieldvalue = addressfloorarea;
            }
            if (addresslotarea != null) {
                c.setAddresslotarea(addresslotarea);
                fieldname = "addresslotarea";
                fieldvalue = addresslotarea;
            }
            if (addressstability != null) {
                c.setAddressstability(addressstability);
                fieldname = "addressstability";
                fieldvalue = addressstability;
            }
            if (addressmodel != null) {
                c.setAddressmodel(addressmodel);
                fieldname = "addressmodel";
                fieldvalue = addressmodel;
            }
            if (addressmake != null) {
                c.setAddressmake(addressmake);
                fieldname = "addressmake";
                fieldvalue = addressmake;
            }
            if (addressland != null) {
                c.setAddressland(addressland);
                fieldname = "addressland";
                fieldvalue = addressland;
            }
            if (addressmonthlyrental != null) {
                c.setAddressmonthlyrental(addressmonthlyrental);
                fieldname = "addressmonthlyrental";
                fieldvalue = addressmonthlyrental;
            }
            if (addresslandlordname != null) {
                c.setAddresslandlordname(addresslandlordname);
                fieldname = "addresslandlordname";
                fieldvalue = addresslandlordname;
            }
            if (addresslandlordaddress != null) {
                c.setAddresslandlordaddress(addresslandlordaddress);
                fieldname = "addresslandlordaddress";
                fieldvalue = addresslandlordaddress;
            }
            if (addresslandlordphoneno != null) {
                c.setAddresslandlordphoneno(addresslandlordphoneno);
                fieldname = "addresslandlordphoneno";
                fieldvalue = addresslandlordphoneno;
            }
            if (spousedegree != null) {
                c.setSpousedegree(spousedegree);
                fieldname = "spousedegree";
                fieldvalue = spousedegree;
            }
            if (spouseschool != null) {
                c.setSpouseschool(spouseschool);
                fieldname = "spouseschool";
                fieldvalue = spouseschool;
            }
            if (spouseyeargraduated != null) {
                c.setSpouseyeargraduated(Integer.parseInt(spouseyeargraduated));
                fieldname = "spouseyeargraduated";
                fieldvalue = spouseyeargraduated;
            }
            if (previousstreetaddress != null) {
                c.setPreviousstreetaddress(previousstreetaddress);
                fieldname = "previousstreetaddress";
                fieldvalue = previousstreetaddress;
            }
            if (previousbarangay != null) {
                c.setPreviousbarangay(previousbarangay);
                fieldname = "previousbarangay";
                fieldvalue = previousbarangay;
            }
            if (previouscityormunicipality != null) {
                c.setPreviouscityormunicipality(previouscityormunicipality);
                fieldname = "previouscityormunicipality";
                fieldvalue = previouscityormunicipality;
            }
            if (previousprovince != null) {
                c.setPreviousprovince(previousprovince);
                fieldname = "previousprovince";
                fieldvalue = previousprovince;
            }
            if (previouspostalcode != null) {
                c.setPreviouspostalcode(previouspostalcode);
                fieldname = "previouspostalcode";
                fieldvalue = previouspostalcode;
            }
            if (previouslengthofstayinyears != null) {
                c.setPreviouslengthofstayinyears(Integer.parseInt(previouslengthofstayinyears));
                fieldname = "previouslengthofstayinyears";
                fieldvalue = previouslengthofstayinyears;
            }
            if (previouslengthofstayinmonths != null) {
                c.setPresentaddresslengthofstayinmonths(Integer.parseInt(previouslengthofstayinmonths));
                fieldname = "previouslengthofstayinmonths";
                fieldvalue = previouslengthofstayinmonths;
            }
            if (parentsstreetaddress != null) {
                c.setParentsstreetaddress(parentsstreetaddress);
                fieldname = "parentsstreetaddress";
                fieldvalue = parentsstreetaddress;
            }
            if (parentsbarangay != null) {
                c.setParentsbarangay(parentsbarangay);
                fieldname = "parentsbarangay";
                fieldvalue = parentsbarangay;
            }
            if (parentscityormunicipality != null) {
                c.setParentscityormunicipality(parentscityormunicipality);
                fieldname = "parentscityormunicipality";
                fieldvalue = parentscityormunicipality;
            }
            if (parentsprovince != null) {
                c.setParentsprovince(parentsprovince);
                fieldname = "parentsprovince";
                fieldvalue = parentsprovince;
            }
            if (parentspostalcode != null) {
                c.setParentspostalcode(parentspostalcode);
                fieldname = "parentspostalcode";
                fieldvalue = parentspostalcode;
            }
            if (parentslengthofstayinyear != null) {
                c.setParentsinlawlengthofstayinyears(Integer.parseInt(parentslengthofstayinyear));
                fieldname = "parentslengthofstayinyear";
                fieldvalue = parentslengthofstayinyear;
            }
            if (parentslengthofstayinmonths != null) {
                c.setParentsinlawlengthofstayinmonths(Integer.parseInt(parentslengthofstayinmonths));
                fieldname = "parentslengthofstayinmonths";
                fieldvalue = parentslengthofstayinmonths;
            }
            if (fatherinlawfirstname != null) {
                c.setFatherinlawfirstname(fatherinlawfirstname);
                fieldname = "fatherinlawfirstname";
                fieldvalue = fatherinlawfirstname;
            }
            if (fatherinlawmiddlename != null) {
                c.setFatherinlawmiddlename(fatherinlawmiddlename);
                fieldname = "fatherinlawmiddlename";
                fieldvalue = fatherinlawmiddlename;
            }
            if (fatherinlawlastname != null) {
                c.setFatherinlawlastname(fatherinlawlastname);
                fieldname = "fatherinlawlastname";
                fieldvalue = fatherinlawlastname;
            }
            if (motherinlawfirstname != null) {
                c.setMotherinlawfirstname(motherinlawfirstname);
                fieldname = "motherinlawfirstname";
                fieldvalue = motherinlawfirstname;
            }
            if (motherinlawmiddlename != null) {
                c.setMotherinlawmiddlename(motherinlawmiddlename);
                fieldname = "motherinlawmiddlename";
                fieldvalue = motherinlawmiddlename;
            }
            if (motherinlawlastname != null) {
                c.setMotherinlawlastname(motherinlawlastname);
                fieldname = "motherinlawlastname";
                fieldvalue = motherinlawlastname;
            }
            if (fatherinlawemployer != null) {
                c.setFatherinlawemployer(fatherinlawemployer);
                fieldname = "fatherinlawemployer";
                fieldvalue = fatherinlawemployer;
            }
            if (motherinlawemployer != null) {
                c.setMotherinlawemployer(motherinlawemployer);
                fieldname = "motherinlawemployer";
                fieldvalue = motherinlawemployer;
            }
            if (fatherinlawposition != null) {
                c.setFatherinlawposition(fatherinlawposition);
                fieldname = "fatherinlawposition";
                fieldvalue = fatherinlawposition;
            }
            if (motherinlawposition != null) {
                c.setMotherinlawposition(motherinlawposition);
                fieldname = "motherinlawposition";
                fieldvalue = motherinlawposition;
            }
            if (fatherinlawage != null) {
                c.setFatherinlawage(Integer.parseInt(fatherinlawage));
                fieldname = "fatherinlawage";
                fieldvalue = fatherinlawage;
            }
            if (motherinlawage != null) {
                c.setMotherinlawage(Integer.parseInt(motherinlawage));
                fieldname = "motherinlawage";
                fieldvalue = motherinlawage;
            }
            if (parentsinlawstreetaddress != null) {
                c.setParentsinlawstreetaddress(parentsinlawstreetaddress);
                fieldname = "parentsinlawstreetaddress";
                fieldvalue = parentsinlawstreetaddress;
            }
            if (parentsinlawbarangay != null) {
                c.setParentsinlawbarangay(parentsinlawbarangay);
                fieldname = "parentsinlawbarangay";
                fieldvalue = parentsinlawbarangay;
            }
            if (parentsinlawcityormunicipality != null) {
                c.setParentsinlawcityormunicipality(parentsinlawcityormunicipality);
                fieldname = "parentsinlawcityormunicipality";
                fieldvalue = parentsinlawcityormunicipality;
            }
            if (parentsinlawprovince != null) {
                c.setParentsinlawprovince(parentsinlawprovince);
                fieldname = "parentsinlawprovince";
                fieldvalue = parentsinlawprovince;
            }
            if (parentsinlawpostalcode != null) {
                c.setParentsinlawpostalcode(parentsinlawpostalcode);
                fieldname = "parentsinlawpostalcode";
                fieldvalue = parentsinlawpostalcode;
            }
            if (parentsinlawlengthofstayinyears != null) {
                c.setParentsinlawlengthofstayinyears(Integer.parseInt(parentsinlawlengthofstayinyears));
                fieldname = "parentsinlawlengthofstayinyears";
                fieldvalue = parentsinlawlengthofstayinyears;
            }
            if (parentsinlawlengthofstayinmonths != null) {
                c.setParentsinlawlengthofstayinmonths(Integer.parseInt(parentsinlawlengthofstayinmonths));
                fieldname = "parentsinlawlengthofstayinmonths";
                fieldvalue = parentsinlawlengthofstayinmonths;
            }
            if (presentaddresslengthofstayinyears != null) {
                c.setPresentaddresslengthofstayinyears(Integer.parseInt(presentaddresslengthofstayinyears));
                fieldname = "presentaddresslengthofstayinyears";
                fieldvalue = presentaddresslengthofstayinyears;
            }
            if (presentaddresslengthofstayinmonths != null) {
                c.setPresentaddresslengthofstayinyears(Integer.parseInt(presentaddresslengthofstayinmonths));
                fieldname = "presentaddresslengthofstayinmonths";
                fieldvalue = presentaddresslengthofstayinmonths;
            }
            if (presentaddressstatus != null) {
                c.setPresentaddressstatus(presentaddressstatus);
                fieldname = "presentaddressstatus";
                fieldvalue = presentaddressstatus;
            }
            crdao.update(c);
            logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Creditinvestigation(Doc #" + c.getDocumentno() + ") with value: " + fieldname + ":" + fieldvalue);
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
                e.printStackTrace();
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }
    //setters

    public void setAddressfloorarea(String addressfloorarea) {
        this.addressfloorarea = addressfloorarea;
    }

    public void setAddressland(String addressland) {
        this.addressland = addressland;
    }

    public void setAddresslandlordaddress(String addresslandlordaddress) {
        this.addresslandlordaddress = addresslandlordaddress;
    }

    public void setAddresslandlordname(String addresslandlordname) {
        this.addresslandlordname = addresslandlordname;
    }

    public void setAddresslandlordphoneno(String addresslandlordphoneno) {
        this.addresslandlordphoneno = addresslandlordphoneno;
    }

    public void setAddresslotarea(String addresslotarea) {
        this.addresslotarea = addresslotarea;
    }

    public void setAddressmake(String addressmake) {
        this.addressmake = addressmake;
    }

    public void setAddressmodel(String addressmodel) {
        this.addressmodel = addressmodel;
    }

    public void setAddressmonthlyrental(String addressmonthlyrental) {
        this.addressmonthlyrental = addressmonthlyrental;
    }

    public void setAddressstability(String addressstability) {
        this.addressstability = addressstability;
    }

    public void setFatherinlawage(String fatherinlawage) {
        this.fatherinlawage = fatherinlawage;
    }

    public void setFatherinlawemployer(String fatherinlawemployer) {
        this.fatherinlawemployer = fatherinlawemployer;
    }

    public void setFatherinlawfirstname(String fatherinlawfirstname) {
        this.fatherinlawfirstname = fatherinlawfirstname;
    }

    public void setFatherinlawlastname(String fatherinlawlastname) {
        this.fatherinlawlastname = fatherinlawlastname;
    }

    public void setFatherinlawmiddlename(String fatherinlawmiddlename) {
        this.fatherinlawmiddlename = fatherinlawmiddlename;
    }

    public void setFatherinlawposition(String fatherinlawposition) {
        this.fatherinlawposition = fatherinlawposition;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setMotherinlawage(String motherinlawage) {
        this.motherinlawage = motherinlawage;
    }

    public void setMotherinlawemployer(String motherinlawemployer) {
        this.motherinlawemployer = motherinlawemployer;
    }

    public void setMotherinlawfirstname(String motherinlawfirstname) {
        this.motherinlawfirstname = motherinlawfirstname;
    }

    public void setMotherinlawlastname(String motherinlawlastname) {
        this.motherinlawlastname = motherinlawlastname;
    }

    public void setMotherinlawmiddlename(String motherinlawmiddlename) {
        this.motherinlawmiddlename = motherinlawmiddlename;
    }

    public void setMotherinlawposition(String motherinlawposition) {
        this.motherinlawposition = motherinlawposition;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setParentsbarangay(String parentsbarangay) {
        this.parentsbarangay = parentsbarangay;
    }

    public void setParentscityormunicipality(String parentscityormunicipality) {
        this.parentscityormunicipality = parentscityormunicipality;
    }

    public void setParentsinlawbarangay(String parentsinlawbarangay) {
        this.parentsinlawbarangay = parentsinlawbarangay;
    }

    public void setParentsinlawcityormunicipality(String parentsinlawcityormunicipality) {
        this.parentsinlawcityormunicipality = parentsinlawcityormunicipality;
    }

    public void setParentsinlawlengthofstayinmonths(String parentsinlawlengthofstayinmonths) {
        this.parentsinlawlengthofstayinmonths = parentsinlawlengthofstayinmonths;
    }

    public void setParentsinlawlengthofstayinyears(String parentsinlawlengthofstayinyears) {
        this.parentsinlawlengthofstayinyears = parentsinlawlengthofstayinyears;
    }

    public void setParentsinlawpostalcode(String parentsinlawpostalcode) {
        this.parentsinlawpostalcode = parentsinlawpostalcode;
    }

    public void setParentsinlawprovince(String parentsinlawprovince) {
        this.parentsinlawprovince = parentsinlawprovince;
    }

    public void setParentsinlawstreetaddress(String parentsinlawstreetaddress) {
        this.parentsinlawstreetaddress = parentsinlawstreetaddress;
    }

    public void setParentslengthofstayinmonths(String parentslengthofstayinmonths) {
        this.parentslengthofstayinmonths = parentslengthofstayinmonths;
    }

    public void setParentslengthofstayinyear(String parentslengthofstayinyear) {
        this.parentslengthofstayinyear = parentslengthofstayinyear;
    }

    public void setParentspostalcode(String parentspostalcode) {
        this.parentspostalcode = parentspostalcode;
    }

    public void setParentsprovince(String parentsprovince) {
        this.parentsprovince = parentsprovince;
    }

    public void setParentsstreetaddress(String parentsstreetaddress) {
        this.parentsstreetaddress = parentsstreetaddress;
    }

    public void setPresentaddresslengthofstayinmonths(String presentaddresslengthofstayinmonths) {
        this.presentaddresslengthofstayinmonths = presentaddresslengthofstayinmonths;
    }

    public void setPresentaddresslengthofstayinyears(String presentaddresslengthofstayinyears) {
        this.presentaddresslengthofstayinyears = presentaddresslengthofstayinyears;
    }

    public void setPresentaddressstatus(String presentaddressstatus) {
        this.presentaddressstatus = presentaddressstatus;
    }

    public void setPreviousbarangay(String previousbarangay) {
        this.previousbarangay = previousbarangay;
    }

    public void setPreviouscityormunicipality(String previouscityormunicipality) {
        this.previouscityormunicipality = previouscityormunicipality;
    }

    public void setPreviouslengthofstayinmonths(String previouslengthofstayinmonths) {
        this.previouslengthofstayinmonths = previouslengthofstayinmonths;
    }

    public void setPreviouslengthofstayinyears(String previouslengthofstayinyears) {
        this.previouslengthofstayinyears = previouslengthofstayinyears;
    }

    public void setPreviouspostalcode(String previouspostalcode) {
        this.previouspostalcode = previouspostalcode;
    }

    public void setPreviousprovince(String previousprovince) {
        this.previousprovince = previousprovince;
    }

    public void setPreviousstreetaddress(String previousstreetaddress) {
        this.previousstreetaddress = previousstreetaddress;
    }

    public void setSpousedegree(String spousedegree) {
        this.spousedegree = spousedegree;
    }

    public void setSpouseschool(String spouseschool) {
        this.spouseschool = spouseschool;
    }

    public void setSpouseyeargraduated(String spouseyeargraduated) {
        this.spouseyeargraduated = spouseyeargraduated;
    }
}
